package com.example.sharedelementdemo

import android.content.Context
import android.view.ViewGroup
import android.widget.ImageView
import coil.Coil
import coil.ImageLoader
import coil.load
import coil.request.ImageRequest
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.QuickViewHolder

class MyAdapter2(val callBack: CoilCallBack) : BaseQuickAdapter<LocalBean, QuickViewHolder>() {
    private var currentPage = 0
    override fun onCreateViewHolder(
        context: Context,
        parent: ViewGroup,
        viewType: Int
    ): QuickViewHolder {
// 返回一个 ViewHolder
        return QuickViewHolder(R.layout.item_layout2, parent)
    }

    override fun onBindViewHolder(holder: QuickViewHolder, position: Int, item: LocalBean?) {
// 设置item数据
        holder.setImageResource(R.id.item_image, item!!.mipmap)

//        if (currentPage == position) {
            val request = ImageRequest.Builder(context)
                .data(item.mipmap)
                .target { drawable ->
                    // Handle the result.
                    holder.setImageDrawable(R.id.item_image, drawable)
                    callBack.loadSuccess()
                }
                .build()
            ImageLoader(context).enqueue(request)
//        } else {
//            holder.getView<ImageView>(R.id.item_image).load(item.mipmap) {
//                crossfade(true)
//            }
//        }
    }

    fun setCurrentPage(page: Int) {
        currentPage = page
    }

    interface CoilCallBack {
        fun loadSuccess()
    }
}